<template>
  <div class="article-container">
    <van-nav-bar
      class="app-nav-bar"
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    ></van-nav-bar>
    <div class="article-wrap">
      <h1 class="title">{{ article.title }}</h1>
      <van-cell center class="user-info">
        <div slot="title" class="name">{{ article.aut_name }}</div>
        <div slot="label" class="pubdate">{{ article.pubdate | relativeTime }}</div>
        <van-image
          class="avatar"
          slot="icon"
          round
          fit="cover"
          :src="article.aut_photo"
        />
        <van-button
        :loading='isFollowLoading'
        @click="handleFollowClick"
        round
        size="small"
        :type="article.is_followed? 'default':'info'"
        class="follow-btn"
        :icon="article.is_followed? '':'plus'">{{ article.is_followed? '已关注': '关注' }}</van-button>
      </van-cell>
      <div class="content markdown-body" v-html="article.content" ref="article-content">
      </div>
      <comment-list :source='articleId' :list='commentList' @reply-click='handleReplyClick'
      @update-total-count='totleCountCommentCount = $event'></comment-list>
    </div>

    <!-- 底部区域 -->
    <div class="article-bottom">
      <van-button class="comment-button" type="default" round size="small" @click="isPostShow = true">写评论</van-button>
      <van-icon
        name="comment-o"
        :info="totleCountCommentCount"
        color="#777"
      />
      <van-icon
        :name="article.is_collected? 'star': 'star-o'"
        @click="handleCollectedClick"
        :color="article.is_collected? 'orange': '#777'"
      />
      <van-icon
        @click="handleLikeClick"
        :color="article.attitude===1? 'red': '#777'"
        :name="article.attitude===1? 'good-job': 'good-job-o'"
      ></van-icon>
      <van-icon color="#777" name="share"></van-icon>
    </div>
    <!-- 发布弹框 -->
    <van-popup
      v-model="isPostShow"
      position="bottom"
    >
    <post-comment :target='articleId' @post-success='hanlePostSuccess' />
    </van-popup>

    <!-- 回复品论 -->
    <van-popup
      v-model="isReplyShow"
      position="bottom"
    >
    <comment-reply :comment='replyComment' @close='isReplyShow = false' v-if="isReplyShow" :article-id='articleId'/>
    <!-- 这里的v-if的目的是让组件随着弹出层的显示进行渲染和销毁的, 防止加载过的组件不重新渲染导致数据不会重新加载的问题 -->
    </van-popup>
  </div>
</template>

<script>
// 在组件获取动态路由参数
// 方式一:
//    this.$router.params.xxx
// 方拾贰: (推荐)
//    props传参
import './github-markdown.css'
import { getArticleById, addArticleCollected, deleteArticleCollected, addArticleLike, deleteArticleLike } from '@/api/article'
import { ImagePreview } from 'vant'
import { addUserFollow, deleteUserFollow } from '@/api/user'
import CommentList from './components/comment-list'
import PostComment from './components/post-coment'
import CommentReply from './components/comment-reply'
export default {
  name: 'ArticleIndex',
  components: {
    CommentList,
    PostComment,
    CommentReply
  },
  props: {
    articleId: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      article: {}, // 文章详情数据集
      isFollowLoading: false,
      isPostShow: false,
      isReplyShow: false,
      commentList: [], // 评论列表
      totleCountCommentCount: 0, // 评论总数
      replyComment: {} // 当前评论项
    }
  },
  computed: {},
  watch: {},
  mounted () {
    this.loadArticle()
  },
  methods: {
    async loadArticle () {
      const { data } = await getArticleById(this.articleId)
      this.article = data.data // 数据改变视图会更新( 但不是立即的, 异步刷新 )

      // 获取文章内容容器
      // const articleContent = this.$refs['article-content']

      // 数据改变影响视图更新(DOM数据),不是立即的(异步)
      // 所以如果需要在修改数据好后马上操作被该影响数据的视图DOM, 需要把这代码放到this.$nextTick() 中
      // 得到所有img标签
      // this.$nextTick()是Vue提供的一个方法
      this.$nextTick(() => {
        // const imgs = articleContent.querySelectorAll('img')
        // console.log(imgs)
        this.handlePreviewImage()
      })
    },
    handlePreviewImage () {
      // 获取文章内容容器
      const articleContent = this.$refs['article-content']
      const imgs = articleContent.querySelectorAll('img')

      const imgPaths = []
      imgs.forEach((element, index) => {
        imgPaths.push(element.src)
        element.onclick = function () {
          ImagePreview({
            images: imgPaths,
            startPosition: index
          })
        }
      })
    },
    // 点击关注或者取消关注
    async handleFollowClick () {
      this.isFollowLoading = true
      if (this.article.is_followed) {
        await deleteUserFollow(this.article.aut_id)
      } else {
        await addUserFollow(this.article.aut_id)
      }
      this.isFollowLoading = false
      this.article.is_followed = !this.article.is_followed
    },
    // 点击收藏或者取消收藏
    async handleCollectedClick () {
      this.$toast.loading({
        message: '操作中....',
        forbidClick: true
      })
      if (this.article.is_collected) {
        await deleteArticleCollected(this.articleId)
      } else {
        await addArticleCollected(this.articleId)
      }
      this.$toast.success(this.article.is_collected ? '取消收藏' : '收藏成功')
      this.article.is_collected = !this.article.is_collected
    },
    // 点赞或者取消点赞
    async handleLikeClick () {
      this.$toast.loading({
        message: '操作中....',
        forbidClick: true
      })
      if (this.article.attitude === 1) {
        await deleteArticleLike(this.articleId)
        this.article.attitude = 0
      } else {
        await addArticleLike(this.articleId)
        this.article.attitude = 1
      }
    },
    // 发送成功的回调
    hanlePostSuccess (data) {
      this.commentList.unshift(data)
      this.totleCountCommentCount++
      this.isPostShow = false
    },
    // 点击回复
    handleReplyClick (comment) {
      this.replyComment = comment
      this.isReplyShow = true
    }
  }
}

</script>
<style lang='less' scoped>
.article-wrap {
  position: fixed;
  top: 46px;
  bottom: 44px;
  left: 0;
  right: 0;
  overflow: auto;
}
.title {
  font-size: 20px;
  color: #3a3a3a;
  padding: 14px;
  margin: 0;
  background-color: #fff;
}
.user-info {
  .name {
    font-size: 12px;
    color: #333;
  }
  .avatar {
    width: 35px;
    height: 35px;
    margin-right: 8px;
  }
  .pubdate {
    font-size: 11px;
    color: #b4b4b4;
  }
  .follow-btn {
    width: 85px;
    height: 29px;
  }
}

.markdown-body {
  padding: 14px;
  background-color: #fff;
}
ul {
  list-style: unset;
}

.article-bottom {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  box-sizing: border-box;
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 44px;
  border-top: 1px solid #d8d8d8;
  background-color: #fff;
  .comment-button {
    width: 160px;
    height: 30px;
    border: 1px solid #eeee;
    font-size: 15px;
    line-height: 23px;
    color: #a7a7a7;
  }
  .van-icon {
    font-size: 20px;
    .van-info {
      font-size: 11px;
      background-color: #e22829;
    }
  }
}
</style>
